﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _07_WebHost.Host
{
    public interface IApplicationBuilder
    {
        void Use(Func<RequestDelegate, RequestDelegate> middleware);
    }
    public class ApplicationBuilder: IApplicationBuilder
    {
        private readonly List<Func<RequestDelegate, RequestDelegate>> _components = new();

        public void Use(Func<RequestDelegate, RequestDelegate> middleware)
        {
            _components.Add(middleware);
        }

       
       
        public RequestDelegate Build()
        {
            RequestDelegate app = c =>
            {
                throw new InvalidOperationException("无效的管道");
            };
            for (int i = _components.Count - 1; i > -1; i--)
            {
                app = _components[i](app);
            }
            return app;
        }
    }
}
